<?php

/*
 * 预报入库业务类
 */

class Application_Model_ingstore extends Application_Model_DomainObject {

    protected $_id; //序号
    protected $_name; //名称
    protected $_type; //来源类型
    protected $_shipTypeid; //快递公司序号
    protected $_shipType; //快递公司
    protected $_shipNo; //快递单号
    protected $_url; //所属商品
    protected $_remark; //备注
    protected $_createtime; //提交时间
    protected $_intime; //到库时间
    protected $_state; //状态 
    protected $_userid; //用户序号
    private   $_logisticsItems; //运单物流信息
    protected $_problemContent; //问题描述
    protected $_ckPass; //是否通过验证
    protected $_isAbnormal;
    protected $_instore_no; //预报号

    /*
     * 新预报的物品
     */
    const STATE_NEW = 0;
    /*
     * 物品已入库
     */
    const STATE_IN = 1;
    /*
     * 物品已删除
     */
    const STATE_OUT = -1;
    /*
     * 获取当前运单物流信息
     */

    public function logisticsItems() {
        if (!empty($this->_logisticsItems)) {
            return $this->_logisticsItems;
        } else {

            if ($this->_shipNo) {
                $this->_logisticsItems = Application_Model_logisticslogItem::getByNo($this->_shipNo);
                return $this->_logisticsItems;
            } else {
                return null;
            }
        }
    }

    public function getstatename() {
        switch ($this->_state) {
            case 0:
                return "等待入库";
                break;
            case 1:
                return "已入云仓库";
                break;
        }
    }

    public static function getItemCount($userid, $state) {

        $bill = new Application_Model_DbTable_ingstore();

        return $bill->getItemCount($userid, $state);
    }

    public function getproducts() {
        return Application_Model_ingstoreproduct::getbyingstoreid($this->id);
    }

    /**
     * 返回数据持久化对象
     * @return Application_Model_DbTable_ingstore
     */
    protected static function getDb() {
        return new Application_Model_DbTable_ingstore();
    }

    /*
     * 获取所有快递公司列表
     */

    public static function getbyUser($userid) {

        $result = Application_Model_ingstore::getDb()->getbyUser($userid);
        return $result;
    }

    public static function cancel($id) {
        if ($id > 0) {
            $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
            $db = $bootstrap->getResource('db');
            $set = array(
                'state' => -1,
            );
            $where = $db->quoteInto('id = ?', $id);
            $rows_affected = $db->update('uuch_ingstore', $set, $where);
            if ($rows_affected > 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 根据指定的条件返回预报入库单
     * @param int $inid
     * @param int $memberId
     * @param date $startTime 开始时间
     * @param date $endTime 结束时间
     * @param int $status 状态
     * @param int $count
     * @param int $offset
     * @return 返回格式为 array('size'=>?,'list'=>?,'pages'=>?,'index'=>?) 
     * size为查询记录的总条数，list为返回的array型数据，pages是指有多少页，index为当前是哪一页 
     */
    public static function getByFilter($inid, $memberId, $startTime, $endTime, $status, $count, $offset) {
        $where = array();
        $and = '';

        if ($memberId != NULL) {
            $where[$and . ' userId = '] = $memberId;
            $and = ' and ';
        }

        if ($startTime != NULL) {
            $where[$and . ' createtime >= '] = $startTime;
            $and = ' and ';
        }

        if ($endTime != NULL) {
            $where[$and . ' createtime <= '] = $endTime;
            $and = ' and ';
        }

        $where[$and . ' state <> '] = -1;
        $and = ' and ';

        if ($status != NULL) {
            $where[$and . ' state = '] = $status;
            $and = ' and ';
        }

        return Application_Model_ingstore::getDb()->queryPage($where, 'createtime desc ', $count, $offset);
    }

    private $shipBy;

    /**
     * 返回对应的快递公司 
     */
    public function getShipTypeBy() {

        if (!isset($this->shipBy)) {
            $this->shipBy = Application_Model_shipType::load($this->_shipTypeid);
        }

        return $this->shipBy;
    }

    /**
     * 获取当日预报入库的数量
     */
    public static function getcountingstore() {
        $where[' createtime >= '] = date('Y-m-d');
        //$where[' and state != '] = -1;
        return Application_Model_ingstore::getDao()->count($where);
    }

    public static function getMyByFilter($username, $storeno, $shipNo, $dateinbegin, $dateinend, $state, $day, $count, $offset) {
        $where = array();
        $and = '';

        $hasUserCon = false;

        $user_where = " 1=1 ";
        if ($username != NULL) {
            $hasUserCon = true;
            $user_where .= " and nikename = '$username' ";
        }
        if ($storeno != null) {
            $hasUserCon = true;
            $user_where .= " and storeno = '$storeno' ";
        }
        if ($hasUserCon) {
            //查询用户
            $array_user = Application_Model_User::getDao()->fetchRow($user_where);
            if ($array_user) {
                $where['userid = '] = $array_user->id;
                $and = ' and ';
            } else {
                return NULL;
            }
        }
        if ($shipNo != NULL) {
            $where[$and . ' shipNo like '] = '%' . $shipNo . '%';
            $and = ' and ';
        }
        if ($day != 1) {
            if ($dateinbegin != NULL) {
                $where[$and . ' createtime >= '] = $dateinbegin;
                $and = ' and ';
            }
            if ($dateinend != NULL) {
                $where[$and . ' createtime <= '] = $dateinend;
                $and = ' and ';
            }
        } else {
            $where[$and . ' createtime >= '] = date('Y-m-d');
            $and = ' and ';
        }
        if ($state != NULL) {
            $where[$and . ' state = '] = $state;
            $and = ' and ';
        }
//        var_dump($where);exit;
        return Application_Model_ingstore::getDb()->queryPage($where, "  id desc ", $count, $offset);
    }

    /**
     * 根据userid，加载user对象
     * @author wuzw
     * @return type
     */
    public function getuserinfo() {
        if ($this->_userid) {
            return Application_Model_User::load($this->_userid);
        }
    }

    /*
     * 根据用户id获取用户仓库号
     */

    public function getbystoreno() {
        $userid = $this->_userid;
        return Application_Model_User::load($userid);
    }

    public function getPayDescription() {
        return '预报包裹';
    }

    public function getPayOnline() {
        return '网购包裹';
    }

    /**
     * 根据物流单号，获取包裹信息
     * @param string $shipNo
     * @return type
     */
    public static function getByShipNo($shipNo) {
        return Application_Model_ingstore::getDao()->queryOne(array('shipNo' => $shipNo));
    }
    
    private $_emmis;
    public function getEmmis()
    {
        if ($this->_emmis == null) {
            $shipItem = Application_Model_shipType::load($this->_shipTypeid);
            if($shipItem && $shipItem->EmId != ""){
                $this->_emmis = Application_Model_Emmis::load($shipItem->EmId);
            }
            if ($this->_emmis == NULL)
                $this->_emmis = new Application_Model_Emmis();
        }
        return $this->_emmis;
    }
    
    public function validShipNo(){
        $result = false;
        if($this->shipNo){
            $result = self::getDao()->validShipNo($this->shipNo);
        }
        return $result;
    }

}

